package com.goldsprite.microhorigame.screens.tests.lightsystem;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.screens.GScreen;
import com.goldsprite.gameframeworks.utils.math.Vector2;

public class HackLightDemo2 extends GScreen {
	int k19;
	private static final int WORLD_WIDTH = 960;
	private static final int WORLD_HEIGHT = 540;
	private OrthographicCamera worldCamera;

	private DayNightLightEngine lightEngine;
	private SpriteBatch batch;

	// 纹理
	private Texture groundTexture, playerTexture, /*光源纹理（圆形渐变）*/lightTexture;

	// 游戏对象位置
	private Vector2 playerCenter;

	// 光源
	private HackLight playerLight;

	private float lightScl = 0.15f;

	private Color gameBackColor = new Color(0, 0, 0, 1);

	@Override
	public Color getScreenBackColor() {
		return gameBackColor;
	}

	@Override
	public void create() {
		// 初始化批处理
		batch = new SpriteBatch();

		worldCamera = new OrthographicCamera();

		// 加载纹理（请确保这些图片在assets目录中）
		groundTexture = new Texture("sprites/blocks/Mockup.png");    // 地面纹理
		playerTexture = new Texture("sprites/blocks/SeveredFang.png");    // 玩家角色
		lightTexture = new Texture("sprites/light3.png");      // 圆形渐变光源纹理

		// 创建光源纹理区域
		TextureRegion lightRegion = new TextureRegion(lightTexture);

		// 初始化昼夜交替光照引擎
		lightEngine = new DayNightLightEngine();
		lightEngine.createFBO(WORLD_WIDTH, WORLD_HEIGHT);

		// 初始化游戏对象
		playerCenter = new Vector2(WORLD_WIDTH / 2f, WORLD_HEIGHT / 2f);

		// 创建玩家跟随的光源（不受昼夜循环影响）
		playerLight = new HackLight(lightRegion, Color.valueOf("#8DEAE8DD"), 2f * lightScl);
		playerLight.setCenter(playerCenter.x, playerCenter.y);
		lightEngine.addLight(playerLight); // 普通添加，不受管理

		// 设置循环速度（可选）
		lightEngine.setDayDuration(30); // 秒
		lightEngine.setTime(0);
	}

	int k41;
	@Override
	public void render(float delta) {
		//ScreenUtils.clear(getClearScreenColor());

		logic(delta);

		// 1. 开始场景捕获
		lightEngine.beginSceneCapture();

		//绘制背景
		drawScreenBack();

		// 2. 渲染游戏场景到场景FBO
		batch.begin();
		worldCamera.update();
		batch.setProjectionMatrix(worldCamera.combined);

		//绘制场景示例图
		float height = WORLD_HEIGHT;
		float width = WORLD_WIDTH*1.0f/WORLD_HEIGHT * groundTexture.getWidth();
		batch.draw(groundTexture, 0, 0, width, height);

//		// 绘制地面...
//		for (int x = 0; x < WORLD_WIDTH; x += groundTexture.getWidth()) {
//			for (int y = 0; y < WORLD_HEIGHT; y += groundTexture.getHeight()) {
//				batch.draw(groundTexture, x, y);
//			}
//		}
		// 绘制玩家...
		batch.draw(playerTexture,
				   playerCenter.x - playerTexture.getWidth() / 2f,
				   playerCenter.y - playerTexture.getHeight() / 2f);
		batch.end();

		// 3. 结束场景捕获
		lightEngine.endSceneCapture();

		// 4. 使用光照引擎混合渲染
		OrthographicCamera cam = worldCamera;
		lightEngine.draw(cam.combined, getViewport(), getClearScreenColor());

		// 更新昼夜循环
		lightEngine.updateDayNightCycle(delta);
	}

	Vector2 tmpVec2 = new Vector2();
	private void logic(float delta) {
		handleInput(delta);

		// 更新玩家光源位置
		playerLight.setCenter(playerCenter.x, playerCenter.y);

		//相机
		worldCamera.position.set(playerCenter.x, playerCenter.y, 0);
		Debug.info("worldCameraPos: %s", worldCamera.position);
		worldCamera.update();
		worldCamera.zoom = 0.9f;
	}


	private void handleInput(float delta) {
		float speed = 400 * delta;

		if (Gdx.input.isKeyPressed(Input.Keys.W) || Gdx.input.isKeyPressed(Input.Keys.UP)) {
			playerCenter.y += speed;
		}
		if (Gdx.input.isKeyPressed(Input.Keys.S) || Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
			playerCenter.y -= speed;
		}
		if (Gdx.input.isKeyPressed(Input.Keys.A) || Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			playerCenter.x -= speed;
		}
		if (Gdx.input.isKeyPressed(Input.Keys.D) || Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			playerCenter.x += speed;
		}

		// 限制玩家在屏幕内
		playerCenter.x = MathUtils.clamp(playerCenter.x, 0, WORLD_WIDTH);
		playerCenter.y = MathUtils.clamp(playerCenter.y, 0, WORLD_HEIGHT);
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		worldCamera.setToOrtho(false, getViewSize().x, getViewSize().y);
	}
}
